/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       bitmap_pool.cpp

	$Header: /game/bitmap_pool.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "bitmap_pool.h"

#include <algorithm>
#include "bitmap_group_cache.h"
#include "string_insensitive_compare.h"

namespace 
{
	struct t_sort
	{
		t_bitmap_pool const& m_pool;

		t_sort( t_bitmap_pool const& pool ) : m_pool( pool )
		{
		}

		bool operator()( int left, int right ) const
		{
			t_bitmap_layer_cache left_cache = m_pool.get_cache(left);
			t_bitmap_layer_cache right_cache = m_pool.get_cache(right);

			return string_insensitive_compare( left_cache.get_name(), 
				                               right_cache.get_name() ) < 0;
		}
	};

	struct t_find
	{
		t_bitmap_pool const& m_pool;

		t_find( t_bitmap_pool const& pool ) : m_pool( pool )
		{
		};

		bool operator()( int left, std::string const& name ) const
		{
			t_bitmap_layer_cache left_cache = m_pool.get_cache(left);

			return (string_insensitive_compare( left_cache.get_name(), name ) < 0);
		}
	};

}; // unnammed namespace


t_bitmap_pool::t_bitmap_pool()
{
}

// --------------------------------------------------------------------
// class to return bitmap_layer_ptr's from multiple bitmap group sources
// construct with reference to bitmap_group_cache array, and list of 
// layers contained therin.
// --------------------------------------------------------------------
t_bitmap_pool::t_bitmap_pool( t_bitmap_group_cache const* caches, t_char_ptr_pair const* pairs, int count )
{
	int i = 0;

  for ( i = 0; i < count; i++ )
  {
    t_bitmap_layer_cache cache( caches[pairs[i].value], pairs[i].keyword );

    m_bitmaps.push_back( cache );
    m_index.push_back( i );
  }

	// provide a sort index.
	std::sort( m_index.begin(), m_index.end(), t_sort( *this ) );
}


// --------------------------------------------------------------------
// class to return bitmap_layer_ptr's from multiple bitmap group sources
// construct with reference to bitmap_group_cache array, and list of 
// layers contained therin.
// --------------------------------------------------------------------
t_bitmap_pool::t_bitmap_pool( t_bitmap_group_cache const* caches, t_string_pair const* pairs, int count )
{
	int i = 0;
  
  for ( i = 0; i < count; i++ )
  {
    t_bitmap_layer_cache cache( caches[pairs[i].value], pairs[i].keyword );
    m_bitmaps.push_back( cache );
    m_index.push_back( i );    
  }

	// provide a sort index.
	std::sort( m_index.begin(), m_index.end(), t_sort( *this ) );
}

int t_bitmap_pool::find( std::string const& name ) const
{
	std::vector<int>::const_iterator result;
	
	result = std::lower_bound( m_index.begin(), m_index.end(), name, t_find( *this ) );
	if (result == m_index.end())
		return -1;

	t_bitmap_layer_cache result_cache = m_bitmaps[*result];

	if (string_insensitive_compare( result_cache.get_name(), name ) != 0)
		return -1;
	return *result;
}


t_bitmap_layer_ptr t_bitmap_pool::get( int id ) const
{
	if (id < 0)
		return t_bitmap_layer_ptr();
	return m_bitmaps[id].get();
}


